import services from '@/services/quartz';
import {ActionType, PageContainer, ProColumns, ProTable,} from '@ant-design/pro-components';
import {Button, Tooltip} from 'antd';
import React, {useRef, useState} from 'react';
import UploadJar from './components/UploadJar';

const {pageClazzMeta} = services.QrtzClazzMetaController;



const TableList: React.FC<unknown> = () => {
    const actionRef = useRef<ActionType>();
    const [current, setCurrent] = useState(1); // 当前页码
    const [pageSize, setPageSize] = useState(10); // 每页显示条数
    // 上传工作jar
    const [jarModalVisible, handleJarModalVisible] = useState<boolean>(false);

    const columns: ProColumns<API.QrtzClazzMetaEntity>[] = [
        {
            title: 'id',
            dataIndex: 'id',
            tooltip: '唯一的 key',
            hideInForm: true,
            hideInTable: true,
            search: false
        },
        {
            title: '元数据名',
            dataIndex: 'name',
        },
        {
            title: '元数据名类路径',
            dataIndex: 'clazz',
            search: false,
        },
        {
            title: '备注',
            dataIndex: 'description',
            hideInForm: true,
            search: false,
        },
    ];

    return (
        <PageContainer
            header={{
                title: '',
            }}
        >
            <ProTable<API.QrtzClazzMetaEntity>
                headerTitle="查询元数据"
                actionRef={actionRef}
                rowKey="id"
                toolBarRender={() => [
                    <Tooltip placement="topLeft" title="上传的jar会被扫描后自动注册里面的job元数据">
                        <Button
                            key="1"
                            type="primary"
                            onClick={() => handleJarModalVisible(true)}
                        >
                            上传任务JAR
                        </Button>,
                     </Tooltip>
                ]}
                search={{
                    labelWidth: 120,
                }}
                pagination={{
                    current, // 当前页码
                    pageSize, // 每页条数
                    showSizeChanger: true, // 是否可以改变 pageSize
                    pageSizeOptions: ['5', '10', '20', '50'], // 自定义每页条数下拉选项
                    onChange: (page, pageSize) => { // 页码或 pageSize 改变的回调。
                      setCurrent(page);
                      setPageSize(pageSize);
                    },
                    onShowSizeChange: (current, size) => {// pageSize 改变的回调。
                      setCurrent(current);
                      setPageSize(size);
                    },
                  }}
                // , sorter, filter
                request={async (params) => {
                    const find: API.QrtzClazzMetaPage = {
                        name: params.name ? params.name : undefined,
                        page: {
                            pageIndex: params.current,
                            pageSize: params.pageSize,
                        },
                    };
                    const {data, success} = await pageClazzMeta({
                        ...find,
                        // FIXME: remove @ts-ignore
                        // @ts-ignore
                        // sorter,
                        // filter,
                    });
                    return {
                        data: data?.rows || [],
                        total: data?.total,
                        pageSize: data?.pageSize,
                        current: data?.currentPage,
                        success,
                    };
                }}
                columns={columns}
            />
            <UploadJar
                modalVisible={jarModalVisible}
                onSubmit={async (value) => {
                    // pf4j
                    console.log(value)
                   alert("当前功能还未实现，敬请期待")
                }}
                onOpenChange={(visible) => {
                    if (!visible) {
                        handleJarModalVisible(false);
                    }
                }}
            />
        </PageContainer>
    );
};

export default TableList;
